namespace RivercrabNet
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;

    public class PaintLineTimeRuler : PaintLineBase
    {
        public PaintLineTimeRuler()
        {
            base.LineType = PaintLineType.TimeRuler;
            base.PlName_zh = "时间尺";
        }

        public override void BeginMove()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                base.BeginMovePoint = crossHairPoint;
                base.MoveType = 0;
                PaintLineBase.PointValue value2 = base.PointList[0];
                float num = base.CgInstance.GetY(base.OwnedPanelID, value2.Value, null);
                PaintLineBase.PointValue value3 = base.PointList[1];
                float num2 = base.CgInstance.GetY(base.OwnedPanelID, value3.Value, null);
                PaintLineBase.PointValue value4 = base.PointList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = base.PointList[1];
                int num4 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                float x = base.CgInstance.GetX(pos);
                float num6 = base.CgInstance.GetX(num4);
                RectangleF ef = new RectangleF(x - (base.LineWidth * 2.5f), num - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                RectangleF ef2 = new RectangleF(num6 - (base.LineWidth * 2.5f), num2 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5));
                if (ef.Contains((PointF) crossHairPoint))
                {
                    base.MoveType = 1;
                    base.CgInstance.Cursor = Cursors.SizeNS;
                }
                else if (ef2.Contains((PointF) crossHairPoint))
                {
                    base.MoveType = 2;
                    base.CgInstance.Cursor = Cursors.SizeNS;
                }
                else
                {
                    base.MoveType = 0;
                    base.CgInstance.Cursor = Cursors.Hand;
                }
                base.BeginMoveValue.Clear();
                base.BeginMoveValue.Add(base.PointList[0]);
                base.BeginMoveValue.Add(base.PointList[1]);
            }
        }

        public override void Draw(Graphics g, List<PaintLineBase.PointValue> pList, Color curColor)
        {
            if (pList.Count != 0)
            {
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                PaintLineBase.PointValue value2 = pList[0];
                float num = (base.CgInstance.GetY(base.OwnedPanelID, value2.Value, null) - div.Rect.Y) - div.TitleHeight;
                PaintLineBase.PointValue value3 = pList[1];
                float num2 = (base.CgInstance.GetY(base.OwnedPanelID, value3.Value, null) - div.Rect.Y) - div.TitleHeight;
                PaintLineBase.PointValue value4 = pList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = pList[1];
                int num4 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                Brush brush = new SolidBrush(curColor);
                Pen pen = new Pen(Color.FromArgb(150, curColor)) {
                    Width = (float) base.LineWidth
                };
                if (base.LineStyle != null)
                {
                    pen.DashPattern = base.LineStyle;
                }
                float num5 = base.CgInstance.GetX(pos) - base.CgInstance.LeftYScaleWidth;
                float num6 = base.CgInstance.GetX(num4) - base.CgInstance.LeftYScaleWidth;
                double[] numArray = this.LineParam(pList);
                float num7 = (base.CgInstance.GetY(base.OwnedPanelID, numArray[0], null) - div.Rect.Y) - div.TitleHeight;
                float num8 = (base.CgInstance.GetY(base.OwnedPanelID, numArray[1], null) - div.Rect.Y) - div.TitleHeight;
                float num9 = (base.CgInstance.GetY(base.OwnedPanelID, numArray[2], null) - div.Rect.Y) - div.TitleHeight;
                float num10 = (base.CgInstance.GetY(base.OwnedPanelID, numArray[3], null) - div.Rect.Y) - div.TitleHeight;
                if (num < num7)
                {
                    num7 = num;
                }
                if (num > num8)
                {
                    num8 = num;
                }
                if (num2 < num9)
                {
                    num9 = num2;
                }
                if (num2 > num10)
                {
                    num10 = num2;
                }
                g.DrawLine(pen, num5, num7, num5, num8);
                g.DrawLine(pen, num6, num9, num6, num10);
                string text = Math.Abs((int) (num4 - pos)).ToString() + "(T)";
                Font font = new Font("New Times Roman", 10f);
                StringFormat format = new StringFormat();
                SizeF ef = g.MeasureString(text, font, 0x3e8, format);
                g.DrawString(text, font, brush, new PointF(((num6 + num5) / 2f) - (ef.Width / 2f), num - (ef.Height / 2f)));
                if (Math.Abs((float) (num5 - num6)) > ef.Width)
                {
                    if (num6 >= num5)
                    {
                        g.DrawLine(pen, num5, num, ((num6 + num5) / 2f) - (ef.Width / 2f), num);
                        g.DrawLine(pen, ((num6 + num5) / 2f) + (ef.Width / 2f), num, num6, num);
                    }
                    else
                    {
                        g.DrawLine(pen, num6, num, ((num6 + num5) / 2f) - (ef.Width / 2f), num);
                        g.DrawLine(pen, ((num6 + num5) / 2f) + (ef.Width / 2f), num, num5, num);
                    }
                }
                if (base.Selected)
                {
                    g.FillRectangle(brush, new RectangleF(num5 - (base.LineWidth * 2.5f), num - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                    g.FillRectangle(brush, new RectangleF(num6 - (base.LineWidth * 2.5f), num2 - (base.LineWidth * 2.5f), (float) (base.LineWidth * 5), (float) (base.LineWidth * 5)));
                }
                brush.Dispose();
                pen.Dispose();
                format.Dispose();
            }
        }

        public override bool Init(Point mp)
        {
            if (base.CgInstance.DataSource.Rows.Count > 0)
            {
                int indexByPoint = base.CgInstance.GetIndexByPoint(mp);
                double valueByPoint = base.CgInstance.GetValueByPoint(mp);
                string key = base.CgInstance.DataSource.Rows[indexByPoint][base.CgInstance.CalenderScaleField].ToString();
                base.PointList.Clear();
                base.PointList.Add(new PaintLineBase.PointValue(0, key, valueByPoint));
                base.PointList.Add(new PaintLineBase.PointValue(1, key, valueByPoint));
                return true;
            }
            return false;
        }

        public override bool JudgeSelect()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv local1 = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                PaintLineBase.PointValue value2 = base.PointList[0];
                float num = base.CgInstance.GetY(base.OwnedPanelID, value2.Value, null);
                PaintLineBase.PointValue value3 = base.PointList[1];
                float num2 = base.CgInstance.GetY(base.OwnedPanelID, value3.Value, null);
                PaintLineBase.PointValue value4 = base.PointList[0];
                int pos = base.CgInstance.GetRecordByTime(value4.Key) - 1;
                PaintLineBase.PointValue value5 = base.PointList[1];
                int num4 = base.CgInstance.GetRecordByTime(value5.Key) - 1;
                float x = base.CgInstance.GetX(pos);
                float num6 = base.CgInstance.GetX(num4);
                double[] numArray = this.LineParam();
                float num7 = base.CgInstance.GetY(base.OwnedPanelID, numArray[0], null);
                float num8 = base.CgInstance.GetY(base.OwnedPanelID, numArray[1], null);
                float num9 = base.CgInstance.GetY(base.OwnedPanelID, numArray[2], null);
                float num10 = base.CgInstance.GetY(base.OwnedPanelID, numArray[3], null);
                if (num < num7)
                {
                    num7 = num;
                }
                if (num > num8)
                {
                    num8 = num;
                }
                if (num2 < num9)
                {
                    num9 = num2;
                }
                if (num2 > num10)
                {
                    num10 = num2;
                }
                if (((crossHairPoint.X >= (x - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (x + (base.LineWidth * 2.5f)))) && ((crossHairPoint.Y >= (num7 - 2f)) && (crossHairPoint.Y <= (num8 + 2f))))
                {
                    return true;
                }
                if (((crossHairPoint.X >= (num6 - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (num6 + (base.LineWidth * 2.5f)))) && ((crossHairPoint.Y >= (num9 - (base.LineWidth * 2.5f))) && (crossHairPoint.Y <= (num10 + (base.LineWidth * 2.5f)))))
                {
                    return true;
                }
                if ((crossHairPoint.Y >= (num - (base.LineWidth * 2.5f))) && (crossHairPoint.Y <= (num + (base.LineWidth * 2.5f))))
                {
                    float num11 = (x >= num6) ? x : num6;
                    float num12 = (x < num6) ? x : num6;
                    if ((crossHairPoint.X >= (num12 - (base.LineWidth * 2.5f))) && (crossHairPoint.X <= (num11 + (base.LineWidth * 2.5f))))
                    {
                        return true;
                    }
                }
            }
            return false;
        }

        public override double[] LineParam()
        {
            return this.LineParam(base.PointList);
        }

        public double[] LineParam(List<PaintLineBase.PointValue> pList)
        {
            if (pList.Count == 0)
            {
                return null;
            }
            PaintLineBase.PointValue value2 = pList[0];
            int num = base.CgInstance.GetRecordByTime(value2.Key) - 1;
            PaintLineBase.PointValue value3 = pList[1];
            int num2 = base.CgInstance.GetRecordByTime(value3.Key) - 1;
            double num3 = base.CgInstance.GetExtremValueByRecord(num + 1, base.OwnedPanelID, 0);
            double num4 = base.CgInstance.GetExtremValueByRecord(num + 1, base.OwnedPanelID, 1);
            double num5 = base.CgInstance.GetExtremValueByRecord(num2 + 1, base.OwnedPanelID, 0);
            double num6 = base.CgInstance.GetExtremValueByRecord(num2 + 1, base.OwnedPanelID, 1);
            return new double[] { num3, num4, num5, num6 };
        }

        public override void Move()
        {
            if (base.PointList.Count != 0)
            {
                Point crossHairPoint = base.CgInstance.GetCrossHairPoint();
                ChartDiv div = base.CgInstance.DicChartDiv[base.OwnedPanelID];
                if ((crossHairPoint.Y >= (div.Rect.Y + div.TitleHeight)) && (crossHairPoint.Y <= (div.Rect.Bottom - div.CalenderScale.Height)))
                {
                    int mouseOverIndex = base.CgInstance.GetMouseOverIndex();
                    double mouseOverValue = base.CgInstance.GetMouseOverValue();
                    if (mouseOverIndex < 0)
                    {
                        mouseOverIndex = 0;
                    }
                    if (mouseOverIndex > (base.CgInstance.LastVisibleRecord - 1))
                    {
                        mouseOverIndex = base.CgInstance.LastVisibleRecord - 1;
                    }
                    switch (base.MoveType)
                    {
                        case 0:
                        {
                            double num3 = crossHairPoint.Y - base.BeginMovePoint.Y;
                            double num4 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MaxValue[0];
                            double num5 = base.CgInstance.DicChartDiv[base.OwnedPanelID].MinValue[0];
                            double num6 = (num3 / ((double) base.CgInstance.GetWorkSpaceY(base.OwnedPanelID))) * (num5 - num4);
                            int indexByPoint = base.CgInstance.GetIndexByPoint(base.BeginMovePoint);
                            int num8 = base.CgInstance.GetIndexByPoint(crossHairPoint);
                            PaintLineBase.PointValue value2 = base.BeginMoveValue[0];
                            int num9 = (base.CgInstance.GetRecordByTime(value2.Key) + (num8 - indexByPoint)) - 1;
                            PaintLineBase.PointValue value3 = base.BeginMoveValue[1];
                            int num10 = (base.CgInstance.GetRecordByTime(value3.Key) + (num8 - indexByPoint)) - 1;
                            if (num9 >= 0)
                            {
                                if (num9 > (base.CgInstance.DataSource.Rows.Count - 1))
                                {
                                    num9 = base.CgInstance.DataSource.Rows.Count - 1;
                                }
                            }
                            else
                            {
                                num9 = 0;
                            }
                            if (num10 < 0)
                            {
                                num10 = 0;
                            }
                            else if (num10 > (base.CgInstance.DataSource.Rows.Count - 1))
                            {
                                num10 = base.CgInstance.DataSource.Rows.Count - 1;
                            }
                            PaintLineBase.PointValue value4 = base.BeginMoveValue[0];
                            base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[num9][base.CgInstance.CalenderScaleField].ToString(), value4.Value + num6);
                            PaintLineBase.PointValue value5 = base.BeginMoveValue[1];
                            base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[num10][base.CgInstance.CalenderScaleField].ToString(), value5.Value + num6);
                            return;
                        }
                        case 1:
                        {
                            base.PointList[0] = new PaintLineBase.PointValue(0, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            PaintLineBase.PointValue value6 = base.PointList[1];
                            base.PointList[1] = new PaintLineBase.PointValue(1, value6.Key, mouseOverValue);
                            return;
                        }
                        case 2:
                        {
                            base.PointList[1] = new PaintLineBase.PointValue(1, base.CgInstance.DataSource.Rows[mouseOverIndex][base.CgInstance.CalenderScaleField].ToString(), mouseOverValue);
                            PaintLineBase.PointValue value7 = base.PointList[0];
                            base.PointList[0] = new PaintLineBase.PointValue(0, value7.Key, mouseOverValue);
                            return;
                        }
                    }
                }
            }
        }
    }
}

